<template>
  <div>
    <SearchForm @search="getTableData" @reset="clearSearchForm">
      <el-col :span="8" v-show="$permiss[1392]" :xs="24">
        <el-form-item :label="$t('所属租户')">
          <el-select style="width: 100%" v-model="page.adminUserId" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[1393]">
        <el-form-item :label="$t('APP名称')">
          <el-select
              style="width: 100%"
              v-model="page.productName"
              clearable
          >
            <el-option
                v-for="item in productList"
                :key="item.productId"
                :label="item.appName"
                :value="item.productName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('群呼名称')">
          <el-input style="width:100%" v-model="page.name" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('类型')">
          <el-select
              style="width: 100%"
              v-model="page.type"
              clearable
          >
            <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('状态')">
          <el-select v-model="page.valid" clearable>
            <el-option :value="0" :label="$t('禁用')"></el-option>
            <el-option :value="1" :label="$t('启用')"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </SearchForm>
    <el-card shadow="never" style="margin-top: 10px">
      <Table style="margin-top: 10px" :table-column="tableColumn" @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange" :table-data="tableData" :page="page" :loading="loading">
        <template slot="valid" slot-scope="{row}">
          <el-switch :value="row.valid === 1" :disabled="$permiss[1395] ? false : true"
                     @change="changeStatus(row)"></el-switch>
        </template>
      </Table>
    </el-card>
  </div>
</template>

<script>
import {selectProduct, selectTenant} from "@/api/system";
import {voiceCallList, voiceCallUpdateStatus} from "@/api/system"
import {marketDeleteStaff} from "@/api/marketing";

export default {
  name: "voiceCall",
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      tenantList: [],
      tableColumn: [
        {label: this.$t('群呼名称'), prop: 'name', minWidth: 100},
        {label: this.$t('APP名称'), prop: 'appName', minWidth: 100},
        {label: this.$t('国号'), prop: 'countryCode', minWidth: 100},
        {label: this.$t('客服电话'), prop: 'servicePhone', minWidth: 100},
        {label: this.$t('显示电话号码'), prop: 'showPhone', minWidth: 100},
        {label: this.$t('类型'), prop: 'type', minWidth: 100},
        {label: this.$t('频率详情'), prop: 'frequency', minWidth: 150},
        {label: this.$t('语言'), prop: 'lang', minWidth: 100},
        {label: this.$t('中文描述'), prop: 'describe', minWidth: 200},
        {label: this.$t('发送内容(对应国家语言)'), prop: 'content', minWidth: 200},
        {label: this.$t('创建时间'), prop: 'creationTime', minWidth: 150},
        {label: this.$t('修改时间'), prop: 'updateTime', minWidth: 150},
        {label: this.$t('状态'), prop: 'valid', slot: 'valid', minWidth: 100},
      ],
      tableData: [],
      loading: false,
      productList: [],
      typeList: [
        {label: this.$t('提前1天提醒'), value: 1},
        {label: this.$t('当天提醒1'), value: 2},
        {label: this.$t('当天提醒2'), value: 3},
        {label: this.$t('逾期1天催收1'), value: 4},
        {label: this.$t('逾期1天催收2'), value: 5},
        {label: this.$t('逾期1天催收3'), value: 6},
        {label: this.$t('逾期1天催收4'), value: 7},
        {label: this.$t('逾期催收'), value: 8},
      ]
    }
  },
  mounted() {
    selectTenant().then(res => {
      this.tenantList = res.data
    })
    selectProduct().then(res => {
      this.productList = res.data
    })
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const res = await voiceCallList(this.page)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list.map(item => {
          item.type = this.typeList.find(v => v.value === item.type).label
          return item
        })
        this.page.total = total
      }
    },
    clearSearchForm() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      }
      this.getTableData()
    },
    async changeStatus({valid, id}) {
      this.$confirm(this.$t('确认禁用?'), this.$t('提示'), {
        type: 'warning'
      }).then(async () => {
        const res = await voiceCallUpdateStatus({valid: valid === 1 ? 0 : 1, id})
        if (res.code === 1) {
          this.$message.success(this.$t('操作成功'))
          this.getTableData()
        }
      }).catch(() => {
      });

    },
    handleCurrentChange(value) {
      this.page.pageNum = value
      this.getTableData()
    },
    handleSizeChange(value) {
      this.page.pageSize = value
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>
